package com.mcxx.modules.miniSalvation.order.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.order.dao.write.InspectionBatchWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.InspectionBatchDTO;
import com.mcxx.modules.miniSalvation.order.entity.InspectionBatchEntity;
import com.mcxx.modules.miniSalvation.order.service.read.OrderInspectionReadService;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@TaTransactional
public class InspectionBatchWriteService {

    @Autowired
    private InspectionBatchWriteDao inspectionBatchWriteDao;
    @Autowired
    private OrderInspectionReadService orderInspectionReadService;
    @Autowired
    private OrderInspectionWriteService orderInspectionWriteService;


    public int insert(InspectionBatchEntity entity) {
        return inspectionBatchWriteDao.insert(entity);
    }


    public int insertIgnoreNull(InspectionBatchEntity entity) {
        return inspectionBatchWriteDao.insertIgnoreNull(entity);
    }


    public int update(InspectionBatchEntity entity) {
        return inspectionBatchWriteDao.update(entity);
    }


    public int updateIgnoreNull(InspectionBatchEntity entity) {
        return inspectionBatchWriteDao.updateIgnoreNull(entity);
    }


    public int delete(InspectionBatchEntity entity) {
        return inspectionBatchWriteDao.delete(entity);
    }


    public void updateBatchState(InspectionBatchEntity batchEntity) {

        if (Constant.InspectionBatchState.UNCONFIRMED.equals(batchEntity.getBatchState())) {
            int count = orderInspectionReadService.countByBatchId(batchEntity.getId());
            if (count > 0) {
                throw new BizException(FailureCode.ERR_80001);
            }
        }


        inspectionBatchWriteDao.updateIgnoreNull(batchEntity);



        orderInspectionWriteService.generateAuditData(batchEntity);
    }


    public void pushBatch(String id, UserAccountVo accountVo) {
        InspectionBatchEntity batchEntity = new InspectionBatchEntity();
        batchEntity.setId(id);
        batchEntity.setBatchResult(Constant.BatchPushState.YES);
        batchEntity.setUpdateName(accountVo.getName());
        batchEntity.setUpdateTime(new Date());

        inspectionBatchWriteDao.updateIgnoreNull(batchEntity);
    }
}
